package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//Dao => Data Access Object 数据访问对象
//通过BlogDao来完成针对blog表的操作
public class BlogDao {

    //1、新增操作（提交博客）
    public void insert(Blog blog){
        Connection connection=null;
        PreparedStatement ps=null;
        //1、建立连接
        try {
            connection=DBUtil.getConnection();
            //2、构造sql
            String sql="insert into blog values(null,?,?,now(),?)";
            ps= connection.prepareStatement(sql);
            ps.setString(1,blog.getTitle());
            ps.setString(2,blog.getContent());
            ps.setInt(3,blog.getUserId());
            //3、执行SQL
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,ps,null);
        }
    }

    //2、查询博客操作（博客列表页）
    //把数据库里所有的博客都拿到
    public List<Blog> getBlogs(){
        List<Blog> blogList =new ArrayList<>();
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog order by postTime desc";
            ps=connection.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                Blog blog=new Blog();
                blog.setBlogId(rs.getInt("blogId"));
                blog.setTitle(rs.getString("title"));
                //文章内容只做摘要
                String content=rs.getString("content");
                if (content.length()>100)
                    content=content.substring(0,100)+"...";
                blog.setContent(content);
                blog.setPostTime(rs.getTimestamp("postTime"));
                blog.setUserId(rs.getInt("userId"));
                blogList.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,ps,rs);
        }
        return blogList;
    }

    //3、根据博客id查询指定内容
    public Blog getBlog(int blogId){
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog where blogId=?";
            ps=connection.prepareStatement(sql);
            ps.setInt(1,blogId);
            rs=ps.executeQuery();
            //博客Id是唯一的，通过博客Id只能搜出来一个结果集，不用循环处理
            if (rs.next()){
                Blog blog=new Blog();
                blog.setBlogId(rs.getInt("blogId"));
                blog.setTitle(rs.getString("title"));
                blog.setContent(rs.getString("content"));
                blog.setPostTime(rs.getTimestamp("postTime"));
                blog.setUserId(rs.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,ps,rs);
        }
        return null;
    }

    //4、根据博客id删除博客
    public void delete(int blogId){
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection=DBUtil.getConnection();
            String sql="delete from blog where blogId=?";
            ps=connection.prepareStatement(sql);
            ps.setInt(1,blogId);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,ps,rs);
        }
    }
}



















